package com.sgb.kjwl.view.ui.activity.self.websiteManage;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.recyclerview.widget.GridLayoutManager;
import android.util.Log;
import android.view.View;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityWebsitePhoto3Binding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.self.WebsitePhotoEntity;
import com.sgb.kjwl.model.entity.self.WebsitePhotoListEntity;
import com.sgb.kjwl.view.ui.adapter.self.website.WebsitePhotoAdapter3;
import com.sgb.kjwl.viewmodel.self.website.WebsitePhotoModel3;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;


public class WebsitePhotoActivity3 extends AppActivity {

    private ActivityWebsitePhoto3Binding binding;
    private WebsitePhotoAdapter3 photoAdapter;

    //图片上传
    private List<LocalMedia> selectList = new ArrayList<>();
    private ArrayList<String> urls;
    private ArrayList<File> fileList;
    private ArrayList<String> netPhotoList;
    private ArrayList<String> uploadImgList;
    private int currentImgNum = 0;
    private int imgTotal = 0;
    private String companyId = "";
    private List<WebsitePhotoEntity> photoList;

    @Inject
    WebsitePhotoModel3 photoModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    public static void start(Context context,String id){
        Intent intent = new Intent(context, WebsitePhotoActivity3.class);
        intent.putExtra("companyId",id);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder()
                .appComponent(appComponent)
                .selfModule(new SelfModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_website_photo3);
        binding.include.ivBack.setOnClickListener(this);
        binding.include.rightView.setOnClickListener(this);
        binding.include.titleView.setText("企业相册");
        binding.include.rightView.setText("编辑");

        photoAdapter = new WebsitePhotoAdapter3(this);
        binding.listView.setLayoutManager(new GridLayoutManager(this,3));
        binding.listView.setAdapter(photoAdapter);
    }

    @Override
    protected void init() {
        fileList = new ArrayList<>();
        urls = new ArrayList<>();
        netPhotoList = new ArrayList<>();
        uploadImgList = new ArrayList<>();

        companyId = getIntent().getStringExtra("companyId");

        //获取网络照片
        photoModel.getPhotoData(companyId);
    }

    public void getPhotoData(List<WebsitePhotoEntity> list){
        photoList = list;

        netPhotoList.clear();
        for(WebsitePhotoEntity entity:list){
            netPhotoList.add(entity.getUrl());
        }
        photoAdapter.setNetImgList(netPhotoList);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);

        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.right_view:
                if(photoAdapter.getUrls().size()>0){
                    WebsitePhotoEditActivity3.start(WebsitePhotoActivity3.this,photoList,companyId);
                }else{
                    MToast.showToast(WebsitePhotoActivity3.this,"暂无照片");
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == PictureConfig.CHOOSE_REQUEST) {
                // 图片结果回调
                selectList = PictureSelector.obtainMultipleResult(data);
                fileList.clear();
                urls.clear();
                for (LocalMedia localMedia : selectList) {
                    File file = new File(localMedia.getRealPath());
                    fileList.add(file);
                    urls.add(localMedia.getRealPath());
                }
                photoAdapter.setDatas(selectList, urls);
                photoAdapter.setFileList(fileList);

                imgTotal = fileList.size();
                photoModel.uploadMutiFileModel(fileList.get(currentImgNum),currentImgNum+1,imgTotal);
            }
        }
    }

    //单个上传
    public void uploadImgSuccess(String url){
        uploadImgList.add(url);
        if (currentImgNum + 1 == imgTotal) {
            uploadSuccess(uploadImgList);
        } else {
            currentImgNum++;
            photoModel.uploadMutiFileModel(fileList.get(currentImgNum),currentImgNum + 1,imgTotal);
        }
    }

    public void uploadSuccess(List<String> list){
        WebsitePhotoListEntity en = new WebsitePhotoListEntity();
        en.setCompanyNo(companyId);
        en.setAlbumUrls(list);
        photoModel.savePhotoData(en);
        Log.v("uploadFile","success--all--");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(String event) {
        if ("delete_website_photo".equals(event)) {
            //删除后刷新列表
            if(null!=photoAdapter.getUrls()){
                photoAdapter.getUrls().clear();
            }
            photoModel.getPhotoData(companyId);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}